package org.apache.cayenne.extentions.modeler;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.SwingUtilities;

import org.apache.cayenne.di.DIBootstrap;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.di.Module;
import org.apache.cayenne.modeler.Application;
import org.apache.cayenne.modeler.Main;
import org.apache.cayenne.modeler.action.OpenProjectAction;
import org.apache.cayenne.modeler.dialog.datamap.SuperclassUpdateController;
import org.apache.cayenne.modeler.editor.datanode.MainDataNodeEditor;
import org.apache.cayenne.modeler.init.platform.PlatformInitializer;
import org.apache.cayenne.modeler.init.CayenneModelerModule;
import org.apache.cayenne.configuration.server.ServerModule;
import org.apache.cayenne.project.CayenneProjectModule;
import org.apache.cayenne.extentions.modeler.bindings.PluginModelerModule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PluginMain extends Main {
	
	private static Log logger = LogFactory.getLog(PluginMain.class);
	public PluginMain(String[] args) {
        super(args);        
    }
	
//	public static void main(String[] args) {
//		 new PluginMain(args).launch();
//
//  }
	
	
//	public void run(String[]args){
//		
//		configureLogging();
//
//        // check jdk version
//        if (!checkJDKVersion()) {
//            System.exit(1);
//        }
//
//        File projectFile = projectFileFromArgs(args);
//        runModeler(projectFile);
//    }
	
//	
	public static void main(String[] args) {
        try {
            new PluginMain(args).launch();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

	@Override
	protected void launch(){
		 final Injector injector = DIBootstrap
         .createInjector(appendModules(new ArrayList<Module>())); 
 logger.info("Starting CayenneModeler.");
 logger.info("JRE v."
         + System.getProperty("java.version")
         + " at "
         + System.getProperty("java.home"));

 // init look and feel before starting any Swing classes...
 injector.getInstance(PlatformInitializer.class).initLookAndFeel();

 SwingUtilities.invokeLater(new Runnable() {

     public void run() {    	
         PluginApplication application = injector.getInstance(PluginApplication.class);        
         PluginApplication.setInstance(application);
         application.startup();

         // start initial project AFTER the app startup, as we need Application
         // preferences to be bootstrapped.

         File project = initialProjectFromArgs();
         if (project == null) {
             project = initialProjectFromPreferences();
         }

         if (project != null) {
             new OpenProjectAction(application).openProject(project);
         }
     }
 });

	}
    @Override
	 protected Collection<Module> appendModules(Collection<Module> modules) {
        modules.add(new ServerModule("CayenneModeler"));
        modules.add(new CayenneProjectModule());
        modules.add(new PluginModelerModule());

        return modules;
    }

//	@Override
//	protected Application initApplication(File projectFile) {
//		return new PluginApplication(projectFile);
//	}

}